#region Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Threading;

using Engine;
using Engine.Pathfinding;
#endregion

namespace Game
{
    class Level1 : GameScreen
    {
        ContentManager content;
        SpriteFont font;
        PlayerIndex playerIndex;
        ThirdPersonCamera camera;
        Random random;
        float seconds;
        int screenWidth;
        int screenHeight;
        int activeWeaponIndex;

        #region Players
        Player player;
        Sniper sniper;
        #endregion

        #region Textures
        List<Texture2D> weaponTextures;
        Texture2D sniperTexture;

        #endregion

        #region Pathfinding
        SwarmManager swarmmanager;
        AStar astar;
        double closestnodedistance;
        Graph levelGraph = new Graph();
        Node playernode;
        Node leadernode; //para prevenir el error de startnode == endnode
        Node n01; //afuera, izq - conectado a nodo n1
        Node n1;  //entrada izq - conectado a nodos 01,2,3
        Node n2;  //centro cuarto arriba izq - conectado a nodos 1, 2 
        Node n3;  //pasillo cuarto arriba izq-cuarto medio - conectado a nodos 1,2,6
        Node n4;  //centro cuarto arriba - conectado a nodos 5,6
        Node n5;  //entrada arriba derecha - conectado a nodos 4,6
        Node n6;  //pasillo cuarto medio-cuarto arriba - conectado a nodos 3,4,5,7
        Node n7;  //centro cuarto arriba - conectado a nodos 3,6,8,9,13
        Node n8;  //entrada derecha - conectado a nodos 7,9,10
        Node n9;  //centro cuarto abajo - conectado a nodos 7,8,10,11,13
        Node n10; //entrada abajo - conectado a nodos 9,11,8,13
        Node n11; //pasillo cuarto medio-cuarto abajo izq - conectado a nodos 10,12
        Node n12; //centro cuarto abajo izq - conectado a nodos 11
        Node n13; //pasillo cuarto medio-cuarto medio izq - conectado a nodos 7,9,14
        Node n14; //centro cuarto medio izq - conectado a nodos 13
        #endregion

        #region INVENTORY & QUICKMENU
        Inventory inventory;
        QuickMenu quickmenu;
        #endregion INVENTORY & QUICKMENU


        public Level1()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
            swarmmanager = new SwarmManager();            
            astar = new AStar(levelGraph);

            #region Graph Setup
            n01 = levelGraph.AddNode(-5182, 200,- 2000);
            n1 = levelGraph.AddNode(-3860, 200, -1796);
            n2 = levelGraph.AddNode(-2594, 200, -2886);
            n3 = levelGraph.AddNode(-1165, 200, 639);
            n4 = levelGraph.AddNode(1437, 200, -3548);
            n5 = levelGraph.AddNode(4074, 200, -3855);
            n6 = levelGraph.AddNode(1488, 200, -1675);
            n7 = levelGraph.AddNode(1553, 200, 395);
            n8 = levelGraph.AddNode(4152, 200, 671);
            n9 = levelGraph.AddNode(1641, 200, 2738);
            n10 = levelGraph.AddNode(245, 200, 5196);
            n12 = levelGraph.AddNode(-1044, 200, 4286);
            n11 = levelGraph.AddNode(-2604, 200, 4286);
            n13 = levelGraph.AddNode(-1095, 200, 2235);
            n14 = levelGraph.AddNode(-2611, 200, 1463);



            levelGraph.Add2Arcs(n01, n1, 1);
            levelGraph.Add2Arcs(n1, n2, 1);
            levelGraph.Add2Arcs(n1, n3, 1);
            levelGraph.Add2Arcs(n2, n3, 1);
            levelGraph.Add2Arcs(n3, n6, 1);
            levelGraph.Add2Arcs(n3, n7, 1);
            levelGraph.Add2Arcs(n4, n5, 1);
            levelGraph.Add2Arcs(n4, n6, 1);
            levelGraph.Add2Arcs(n5, n6, 1);
            levelGraph.Add2Arcs(n6, n7, 1);
            levelGraph.Add2Arcs(n7, n8, 1);
            levelGraph.Add2Arcs(n7, n9, 1);
            levelGraph.Add2Arcs(n7, n13, 1);
            levelGraph.Add2Arcs(n8, n9, 1);
            levelGraph.Add2Arcs(n8, n10, 1);
            levelGraph.Add2Arcs(n9, n10, 1);
            levelGraph.Add2Arcs(n9, n11, 1);
            levelGraph.Add2Arcs(n9, n13, 1);
            levelGraph.Add2Arcs(n10, n11, 1);
            levelGraph.Add2Arcs(n10, n13, 1);
            levelGraph.Add2Arcs(n11, n12, 1);
            levelGraph.Add2Arcs(n13, n14, 1);



            #endregion

        }

        public override void LoadContent()
        {

            random = new Random();
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            activeWeaponIndex = 0;

            font = content.Load<SpriteFont>("Fonts/font");

            screenWidth = ScreenManager.GraphicsDevice.Viewport.Width;
            screenHeight = ScreenManager.GraphicsDevice.Viewport.Height;

            #region INVENTORY & QUICKMENU
            ItemList.Create();
            inventory = new Inventory(4, new Vector2(70, 0), 1);
            quickmenu = new QuickMenu(2, new Vector2(125, 610));

            ItemList.TextureLoader(content.Load<Texture2D>("Textures/Items"));
            inventory.TextureLoader(content.Load<Texture2D>("Textures/Inventory"), font);
            quickmenu.TextureLoader(content.Load<Texture2D>("Textures/QuickMenu"));
            #endregion INVENTORY & QUICKMENU

            #region Sniper
            if (JoysticksInfo.SniperIndexAssigned)
            {
                sniperTexture = content.Load<Texture2D>("Textures/MiraSniper");
                sniper = new Sniper(sniperTexture, ScreenManager.GraphicsDevice, JoysticksInfo.sniperIndex);
            }
            #endregion

            player = new Player(JoysticksInfo.playerIndex);



            weaponTextures = new List<Texture2D>();
            weaponTextures.Add(content.Load<Texture2D>("Textures/Colt1911"));
            weaponTextures.Add(content.Load<Texture2D>("Textures/Mp5"));
            weaponTextures.Add(content.Load<Texture2D>("Textures/Type95"));


            camera = new ThirdPersonCamera(ScreenManager.GraphicsDevice.Viewport,
                player.Transform.Position - player.Transform.Forward * 2000 +
                player.Transform.Up * 2000, player.Transform.Position);



            // A real game would probably have more content than this sample, so
            // it would take longer to load. We simulate that by delaying for a
            // while, giving you a chance to admire the beautiful loading screen.
            Thread.Sleep(1000);

            // once the load has finished, we use ResetElapsedTime to tell the game's
            // timing mechanism that we have just finished a very long frame, and that
            // it should not try to catch up.
            ScreenManager.Game.ResetElapsedTime();

        }
        public override void UnloadContent()
        {
            content.Unload();
        }

        public override void HandleInput()
        {
            //First we check to see if we are in the pause screen or not, then we do our stuff.
            if (Input.IsPauseGame(ControllingPlayer) || Input.IsNewKeyPress(Keys.Escape))
            {
                ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }
            else
            {
                player.HandleInput();

                #region WeaponsChange
                if (Input.IsNewKeyPress(Keys.D8) ||
                    Input.IsNewButtonPress(Buttons.DPadRight, player.Index, out playerIndex)
                    && !inventory.IsActive)
                {
                    if (activeWeaponIndex > 0)
                    {
                        player.Weapons[activeWeaponIndex].IsActiveWeapon = false;
                        player.Weapons[activeWeaponIndex].IsActiveWeapon = true;
                        player.ActiveWeapon = player.Weapons[activeWeaponIndex - 1];
                        activeWeaponIndex--;
                    }
                    else
                    {
                        player.Weapons[activeWeaponIndex].IsActiveWeapon = false;
                        player.Weapons[player.Weapons.Count - 1].IsActiveWeapon = true;
                        player.ActiveWeapon = player.Weapons[player.Weapons.Count - 1];
                        activeWeaponIndex = player.Weapons.Count - 1;
                    }
                }
                if (Input.IsNewKeyPress(Keys.D9) ||
                    Input.IsNewButtonPress(Buttons.DPadLeft, player.Index, out playerIndex)
                    && !inventory.IsActive)
                {
                    if (activeWeaponIndex < player.Weapons.Count - 1)
                    {
                        player.Weapons[activeWeaponIndex].IsActiveWeapon = false;
                        player.Weapons[activeWeaponIndex + 1].IsActiveWeapon = true;
                        player.ActiveWeapon = player.Weapons[activeWeaponIndex + 1];
                        activeWeaponIndex++;
                    }
                    else
                    {
                        player.Weapons[activeWeaponIndex].IsActiveWeapon = false;
                        player.Weapons[0].IsActiveWeapon = true;
                        player.ActiveWeapon = player.Weapons[0];
                        activeWeaponIndex = 0;
                    }
                }
                #endregion

                //if (Input.IsNewKeyPress(Keys.Tab))
                //    Camera.ActiveCameraNumber -= 1;

                #region INVENTORY & QUICKMENU
                if (Input.IsNewKeyPress(Keys.Down) || 
                    Input.IsNewButtonPress(Buttons.Y, player.Index, out playerIndex))
                    inventory.Activate();

                if (Input.IsNewKeyPress(Keys.Up) ||
                    Input.IsNewButtonPress(Buttons.Y, player.Index, out playerIndex))
                    inventory.DeActivate();

                if (Input.IsNewKeyPress(Keys.Left))
                    inventory.ExploreLeft();

                if (Input.IsNewButtonPress(Buttons.DPadLeft,
                    player.Index, out playerIndex) && inventory.IsActive)
                    inventory.ExploreLeft();

                if (Input.IsNewKeyPress(Keys.Right))
                    inventory.ExploreRight();

                if (Input.IsNewButtonPress(Buttons.DPadRight,
                    player.Index, out playerIndex) && inventory.IsActive)
                    inventory.ExploreRight();

                if (Input.IsNewKeyPress(Keys.Enter))
                    inventory.Use(inventory.GetSelected());

                if (Input.IsNewButtonPress(Buttons.A,
                    player.Index, out playerIndex) && inventory.IsActive)
                    inventory.Use(inventory.GetSelected());

                quickmenu.UpDate(inventory);

                if (Input.IsNewKeyPress(Keys.D1)) inventory.QuickMenu(quickmenu, 0);
                if (Input.IsNewButtonPress(Buttons.DPadUp,
                    player.Index, out playerIndex) && inventory.IsActive)
                    inventory.QuickMenu(quickmenu, 0);

                if (Input.IsNewKeyPress(Keys.D2)) inventory.QuickMenu(quickmenu, 1);
                if (Input.IsNewButtonPress(Buttons.DPadDown,
                    player.Index, out playerIndex) && inventory.IsActive)
                    inventory.QuickMenu(quickmenu, 1);
                

                if (Input.IsNewKeyPress(Keys.T)) quickmenu.Use(inventory, 0);
                if (Input.IsNewButtonPress(Buttons.DPadUp,
                    player.Index, out playerIndex) && !inventory.IsActive)
                    inventory.QuickMenu(quickmenu, 0);

                if (Input.IsNewKeyPress(Keys.W)) quickmenu.Use(inventory, 1);
                if (Input.IsNewButtonPress(Buttons.DPadDown,
                    player.Index, out playerIndex) && !inventory.IsActive)
                    inventory.QuickMenu(quickmenu, 1);
                

                if (Input.IsNewKeyPress(Keys.U)) inventory.UpGrade(inventory.GetSelected());
                if (Input.IsNewButtonPress(Buttons.X,
                    player.Index, out playerIndex) && inventory.IsActive)
                    inventory.UpGrade(inventory.GetSelected());

                //inventory.Add(ItemList.GetItem("One Slice"));

                if (Input.IsNewKeyPress(Keys.S))
                    InventoryManager.SaveFile("inventory", inventory);

                if (Input.IsNewKeyPress(Keys.L))
                    inventory.mInventory = InventoryManager.LoadFile("inventory");

                #endregion INVENTORY & QUICKMENU

            }

        }

        public override void Update(GameTime gametime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gametime, otherScreenHasFocus, coveredByOtherScreen);

            float dt = (float)gametime.ElapsedGameTime.TotalSeconds;
            seconds += (float)gametime.ElapsedGameTime.TotalSeconds;

            if (IsActive)//If the screen is active we do our stuff.
            {
                if (JoysticksInfo.SniperIndexAssigned)
                    sniper.Update(gametime);

                player.Update(gametime, camera.Transform);

                for (int i = 0; i < swarmmanager.mSwarmList.Count; i++)
                {
                    swarmmanager.mSwarmList[i].GetPlayerPos(player.Transform.Position);
                }

                if(seconds < 10)
                {
                    astar.SearchPath(n01, levelGraph.ClosestNode((double)player.Transform.Position.X, (double)player.Transform.Position.Y, (double)player.Transform.Position.Z, out closestnodedistance, false));
                    seconds = 20;
                }
                
                for (int i = 0; i < swarmmanager.mSwarmList.Count; i++)
                {
                    for (int j = 0; j < swarmmanager.mSwarmList[i].mEnemyList.Count; j++)
                    {
                        swarmmanager.mSwarmList[i].mEnemyList[j].Path = ConvertResultToQueue(astar.PathByCoordinates);
                    }
                }

                for (int i = 0; i < swarmmanager.mSwarmList.Count; i++)
                {
                    for (int j = 0; j < swarmmanager.mSwarmList[i].mEnemyList.Count; j++)
                    {
                        if( Vector3.Distance(swarmmanager.mSwarmList[i].mEnemyList[j].Transform.Position, player.Transform.Position) < 250)
                        {
                            player.Life.HealthPoints -= 1;
                        }
                    }
                }


                if (Input.isKeyPressed(Keys.T))
                    camera.ZoomToTarget(10);
                if (Input.isKeyPressed(Keys.R))
                    camera.ZoomToTarget(-10);
                if (Input.isKeyPressed(Keys.LeftAlt) || Input.isButtonPressed(Buttons.RightShoulder, player.Index, out playerIndex))
                    camera.RotateAroundTarget(0.001f * gametime.ElapsedGameTime.Milliseconds);
                if (Input.isKeyPressed(Keys.LeftControl) || Input.isButtonPressed(Buttons.LeftShoulder, player.Index, out playerIndex))
                    camera.RotateAroundTarget(-0.001f * gametime.ElapsedGameTime.Milliseconds);

                camera.Update(player.Transform.Position);

                swarmmanager.Update(gametime);




                #region BulletsCollisions
                for (int e = 0; e < swarmmanager.mSwarmList.Count; e++)
                {
                    for (int i = 0; i < swarmmanager.mSwarmList[e].mEnemyList.Count; i++)
                    {
                        for (int b = 0; b < player.ActiveWeapon.BulletsList.Count; b++)
                        {
                            CheckForCollisions(swarmmanager.mSwarmList[e].mEnemyList[i], player.ActiveWeapon.BulletsList.ElementAt(b));
                        }
                    }
                }

                #endregion

                #region SniperCollisions
                if (JoysticksInfo.SniperIndexAssigned)
                {
                    Ray sniperRay = sniper.CalculateCursorRay(Camera.ProjectionMatrix, Camera.ViewMatrix);

                    foreach(Swarm swarm in swarmmanager.mSwarmList)
                    {
                        foreach (Enemy enemy in swarm.mEnemyList)
                        {
                            if (Helper.RayIntersectsModel(sniperRay, enemy.mModel,
                                enemy.Transform.WorldMatrix) && (Input.leftClickPressed() || Input.IsNewButtonPress(Buttons.RightTrigger, JoysticksInfo.sniperIndex, out playerIndex)))
                            {
                                enemy.Life.IsAlive = false;
                                enemy.Transform.Rotate(Vector3.Up, 1);
                            }
                        }
                    }
                }
                #endregion

                #region ObjectsCollisions
                foreach (Model model in ModelDrawer.mStatues)
                {
                    Matrix[] mTransforms = new Matrix[model.Bones.Count];
                    model.CopyAbsoluteBoneTransformsTo(mTransforms);
                    Matrix[] playerTransforms = new Matrix[ModelDrawer.player.Bones.Count];
                    ModelDrawer.player.CopyAbsoluteBoneTransformsTo(playerTransforms);


                    BoundingSphere modelBS = new BoundingSphere();
                    BoundingSphere aux = new BoundingSphere();

                    foreach (ModelMesh m in model.Meshes)
                    {
                        if (m.Name == "CollisionBox")
                        {
                            Vector3 modelPos = Vector3.Zero;
                            modelPos = Vector3.Transform(modelPos, mTransforms[m.ParentBone.Index] * Matrix.Identity);
                            modelBS.Center = m.BoundingSphere.Center + modelPos;
                            modelBS.Radius = m.BoundingSphere.Radius * 40;

                            foreach (ModelMesh mm in ModelDrawer.player.Meshes)
                            {
                                Vector3 playerPos = Vector3.Zero;
                                playerPos = Vector3.Transform(playerPos, playerTransforms[mm.ParentBone.Index] * player.Transform.WorldMatrix);
                                aux.Center = mm.BoundingSphere.Center + playerPos;
                                aux.Radius = mm.BoundingSphere.Radius * 40;
                                if (modelBS.Intersects(aux))
                                {
                                    player.Transform.Position = player.Transform.LastPosition;
                                }

                            }
                        }
                    }
                }
                #endregion

                #region HouseCollisions
                foreach (Model model in ModelDrawer.mHouse)
                {
                    BoundingBox bb = new BoundingBox();
                    if (model.Tag != "Floor")
                    {
                        bb = ModelDrawer.GetBoundingBoxesFromModel(model);


                        BoundingSphere aux = new BoundingSphere();


                        foreach (ModelMesh mm in ModelDrawer.player.Meshes)
                        {
                            Matrix[] playerTransforms = new Matrix[ModelDrawer.player.Bones.Count];
                            model.CopyAbsoluteBoneTransformsTo(playerTransforms);

                            Vector3 playerPos = Vector3.Zero;
                            playerPos = Vector3.Transform(playerPos, playerTransforms[mm.ParentBone.Index] * player.Transform.WorldMatrix);
                            aux.Center = mm.BoundingSphere.Center + player.Transform.Position;
                            aux.Radius = mm.BoundingSphere.Radius * 45;
                            if (bb.Intersects(aux))
                            {
                                player.Transform.Position = player.Transform.LastPosition;
                            }
                        }
                    }
                }
                #endregion

                #region StatuesItemSale
                foreach (Model model in ModelDrawer.mStatues)
                {

                    if (model.Root.Name == "EstatuaComida")
                        if (Vector3.Distance(model.Root.Transform.Translation, player.Transform.Position) < 300)
                            if (Input.IsNewKeyPress(Keys.M) || Input.IsNewButtonPress(Buttons.A, player.Index, out playerIndex))
                                inventory.Add(ItemList.GetItem("One Slice"));

                    if (model.Root.Name == "EstatuaMinas")
                        if (Vector3.Distance(model.Root.Transform.Translation, player.Transform.Position) < 300)
                            if (Input.IsNewKeyPress(Keys.M) || Input.IsNewButtonPress(Buttons.A, player.Index, out playerIndex))
                                inventory.Add(ItemList.GetItem("Bomb"));

                    if (model.Root.Name == "EstatuaArma")
                        if (Vector3.Distance(model.Root.Transform.Translation, player.Transform.Position) < 300)
                            if (Input.IsNewKeyPress(Keys.M) || Input.IsNewButtonPress(Buttons.A, player.Index, out playerIndex))
                                player.Weapons[player.WeaponIndex].IncreaseBullets(30);

                }
                #endregion

            }
        }

        #region Draw
        public override void Draw(GameTime gameTime)
        {
            #region DeviceSettings
            ScreenManager.GraphicsDevice.RenderState.AlphaBlendEnable = false;
            ScreenManager.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            ScreenManager.GraphicsDevice.RenderState.DestinationBlend = Blend.One;
            ScreenManager.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;

            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.CornflowerBlue, 0, 0);

            //We take the spriteBatch from the ScreenManager
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            #endregion

            //---------------------------------------------------------------------------------
            //Draw

            spriteBatch.Begin();

            #region INVENTORY & QUICKMENU
            inventory.Draw(spriteBatch);
            quickmenu.Draw(spriteBatch);
            #endregion INVENTORY & QUICKMENU

            player.Draw(Camera.ViewMatrix, Camera.ProjectionMatrix);

            swarmmanager.Draw(spriteBatch, Camera.ViewMatrix, Camera.ProjectionMatrix);

            ModelDrawer.DrawHouse(Camera.ViewMatrix, Camera.ProjectionMatrix);
            ModelDrawer.DrawStatues(Camera.ViewMatrix, Camera.ProjectionMatrix);

            //--------------------------------------------------------------------------------------------
            /*Aca abajo dibujamos la mira del arma porque necesita parametros del device
            especificos*/

            #region DeviceSettings
            ScreenManager.GraphicsDevice.RenderState.AlphaBlendEnable = true;
            ScreenManager.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            ScreenManager.GraphicsDevice.RenderState.DestinationBlend = Blend.One;
            ScreenManager.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
            #endregion

            #region Sniper Draw
            if (JoysticksInfo.SniperIndexAssigned)
                sniper.Draw(gameTime, spriteBatch);
            #endregion

            spriteBatch.Draw(weaponTextures[player.WeaponIndex],
                new Vector2(screenWidth / 2 - 420, screenHeight / 2 + 260), Color.White);

            spriteBatch.DrawString(ScreenManager.AmmoFont, "Ammo",
                new Vector2(screenWidth / 2 + 100, screenHeight / 2 + 260), Color.White);

            spriteBatch.DrawString(ScreenManager.AmmoFont, player.ActiveWeapon.BulletsLeft.ToString(),
                new Vector2(screenWidth / 2 +300, screenHeight / 2 + 260), Color.White);

            spriteBatch.DrawString(ScreenManager.AmmoFont, player.ActiveWeapon.BulletsQuantity.ToString(),
                new Vector2(screenWidth / 2+400, screenHeight / 2 + 260), Color.White);

            spriteBatch.End();

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);

        }
        #endregion

        #region Other Methods
        private Queue<Vector3> ConvertResultToQueue(Point3D[] _result)
        {

            Queue<Vector3> result = new Queue<Vector3>();
            for (int i = 0; i < _result.Length; i++)
            {
                Vector3 aux = new Vector3();
                aux.X = (float)_result[i].X;
                aux.Y = (float)_result[i].Y;
                aux.Z = (float)_result[i].Z;
                result.Enqueue(aux);
            }

            return result;
        }

        static void CheckForCollisions(Enemy zombie, Bullet bullet)
        {
            Model zombieModel = ModelDrawer.GetModel("Zombie1");
            Model bulletModel = ModelDrawer.GetModel("Bullet");


            //Matrix[] zombieTransforms = new Matrix[zombieModel.Bones.Count];
            //zombieModel.CopyAbsoluteBoneTransformsTo(zombieTransforms);
            //Matrix[] bulletTransforms = new Matrix[bulletModel.Bones.Count];
            //bulletModel.CopyAbsoluteBoneTransformsTo(bulletTransforms);


            //BoundingSphere zombieBS = new BoundingSphere();
            //BoundingSphere bulletBS = new BoundingSphere();

            //foreach (ModelMesh m in zombieModel.Meshes)
            //{
            //    if (m.Name == "CollisionBox")
            //    {
            //        Vector3 modelPos = Vector3.Zero;
            //        modelPos = Vector3.Transform(modelPos, zombieTransforms[m.ParentBone.Index] * zombie.Transform.WorldMatrix);
            //        zombieBS.Center = m.BoundingSphere.Center + modelPos;
            //        zombieBS.Radius = m.BoundingSphere.Radius * 40;

            //        foreach (ModelMesh mm in bulletModel.Meshes)
            //        {
            //            bulletBS.Center = mm.BoundingSphere.Center + bullet.Position;
            //            bulletBS.Radius = mm.BoundingSphere.Radius * 40;
            //            if (zombieBS.Intersects(bulletBS))
            //            {
            //                zombie.TakeDamage(bullet.mDamage);
            //                bullet.Reset();
            //                return;
            //            }

            //        }
            //    }
            //}


            for (int i = 0; i < zombieModel.Meshes.Count; i++)
            {
                //Check whether the bounding boxes of the two cubes intersect.
                BoundingSphere c1BoundingSphere = zombieModel.Meshes[i].BoundingSphere;
                c1BoundingSphere.Center += zombie.Position;
                
                for (int j = 0; j < bulletModel.Meshes.Count; j++)
                {
                    BoundingSphere c2BoundingSphere = bulletModel.Meshes[j].BoundingSphere;
                    c2BoundingSphere.Center += bullet.Position;

                    if (c1BoundingSphere.Intersects(c2BoundingSphere))
                    {
                        zombie.TakeDamage(bullet.mDamage);
                        bullet.Reset();
                        return;
                    }
                }
            }
        }
        #endregion
    }
}
